package com.yt.order.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.yt.order.models.bo.CreateOrderSaveBO;
import com.yt.order.models.dto.OrderProductRelationDTO;
import com.yt.order.models.service.IOrderProductRelationService;
import com.yt.order.models.vo.OrderInfoVO;
import com.yt.product.models.bo.ProductInfoSaveBO;
import com.yt.product.models.bo.ShoppingCartSaveBO;
import com.yt.product.models.dto.ProductInfoDTO;
import com.yt.product.models.service.IProductInfoService;
import com.yt.user.models.dto.UserAddressDTO;
import com.yt.user.models.service.IUserAddressService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.yt.order.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yt.order.mapper.OrderInfoMapper;
import com.yt.order.models.service.IOrderInfoService;
import com.yt.order.models.bo.OrderInfoSaveBO;
import com.yt.order.models.dto.OrderInfoDTO;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 订单信息控制器
 *
 * @author lvzeming
 * @Date 2025-08-22 21:02:52
 */
@DubboService
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfoDTO> implements IOrderInfoService{

    @Resource
    private YmlConfig ymlConfig;

    @DubboReference
    private IProductInfoService productInfoService;

    @DubboReference
    private IUserAddressService userAddressService;

    @DubboReference
    private IOrderProductRelationService orderProductRelationService;

    @Override
    public boolean add(OrderInfoSaveBO saveBO) {
        OrderInfoDTO dto = new OrderInfoDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    @Override
    @Transactional
    public OrderInfoVO createOrder(CreateOrderSaveBO createOrderSaveBO) {
        //查询地址信息
        Wrapper<UserAddressDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("id",createOrderSaveBO.getAddressId());
        UserAddressDTO userAddressDTO = userAddressService.selectOne(wrapper);


        //保存订单信息
        List<ShoppingCartSaveBO> shoppingCartSaveBOList = createOrderSaveBO.getShoppingCartSaveBOList();
        OrderInfoDTO orderInfoDTO = new OrderInfoDTO();
        orderInfoDTO.setIsAdmin(0);
        orderInfoDTO.setOrderStatus(0);
        orderInfoDTO.setPayStatus(0);
        orderInfoDTO.setReceiverAddress(userAddressDTO.getAddress());
        orderInfoDTO.setReceiverName(userAddressDTO.getContactName());
        orderInfoDTO.setReceiverPhone(userAddressDTO.getContactPhone());
        orderInfoDTO.setUserId(createOrderSaveBO.getUserId());
        orderInfoDTO.setPostFee(createOrderSaveBO.getPostFee());
        orderInfoDTO.setTotalPrice(createOrderSaveBO.getTotalPayPrice());

        Long id = ymlConfig.getPkId();
        orderInfoDTO.setId(id);
        this.insert(orderInfoDTO);

        //保存订单关联关系
        for(ShoppingCartSaveBO saveBO : shoppingCartSaveBOList){
            OrderProductRelationDTO orderProductRelationDTO = new OrderProductRelationDTO();
            orderProductRelationDTO.setOrderId(id);
            orderProductRelationDTO.setProductId(saveBO.getProductId());
            ProductInfoDTO productInfoDTO = productInfoService.selectById(saveBO.getProductId());
            orderProductRelationDTO.setProductName(productInfoDTO.getProductName());
            orderProductRelationDTO.setNum(saveBO.getNum());
            orderProductRelationDTO.setPrice(saveBO.getPrice());
            orderProductRelationDTO.setTotalPrice(saveBO.getTotalPrice());
            Long relationId = ymlConfig.getPkId();
            orderProductRelationDTO.setId(relationId);
            orderProductRelationService.insert(orderProductRelationDTO);
        }

        OrderInfoVO orderInfoVO = new OrderInfoVO();
        BeanUtils.copyProperties(orderInfoDTO,orderInfoVO);
        return orderInfoVO;
    }
}
